package org.assocsp3.web;

import org.assocsp3.modele.Personne;
import org.assocsp3.service.ActiviteService;
import org.assocsp3.service.PersonneService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

/**
 * Handles requests for the application welcome page.
 */
@Controller
@RequestMapping("/index")
public class IndexController {

	private Logger logger = org.slf4j.LoggerFactory.getLogger(IndexController.class);
	
	private PersonneService personneService;
	private ActiviteService activiteService;
	
	@Autowired
    public void setServices(PersonneService personneService,ActiviteService serv) {
        this.personneService = personneService;
        this.activiteService = serv;
    }

	@SuppressWarnings("deprecation")
	@RequestMapping(method = RequestMethod.GET)
	public ModelMap index(WebRequest webRequest) {
		logger.info("Index!");
		Personne p = personneService.findByActiveLogin(webRequest.getUserPrincipal().getName());// renvoie l'utilisateur ayant ce login
		/*  AUTRE FACON D'ACCEDER AU USER CONNECTE
		 * SecurityContextHolder.getContext().getAuthentication().getPrincipal());
		 */
		ModelMap m = new ModelMap();
		m.addObject("vosactivites", personneService.getActivites(p));
		m.addObject("paiements", personneService.getFactures(p)); 
		m.addObject("activites", activiteService.findAll());
		return m;
	}
}
